﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.cms.businesslogic.datatype;

namespace ScDataTypes.DataTypes
{

    public class ContentTreeDataType : umbraco.cms.businesslogic.datatype.AbstractDataEditor
    {

        #region Variables

        ContentTreeview _ContentTreeview = new ContentTreeview();

        #endregion

        #region Properties

        [DataEditorSetting("DocumentType", description = "Allowed document type", defaultValue = "")]
        public string DocumentType
        {
            get { return this._ContentTreeview.DocumentType; }
            set { this._ContentTreeview.DocumentType = value; }
        }

        [DataEditorSetting("Location", description = "Inital location", defaultValue = "")]
        public string Location
        {
            get { return this._ContentTreeview.Location; }
            set { this._ContentTreeview.Location = value; }
        }

        [DataEditorSetting("Document Children", description = "Show document children", defaultValue = "1")]
        public string DocumentChildren
        {
            get { return this._ContentTreeview.DocumentChildren; }
            set { this._ContentTreeview.DocumentChildren = value; }
        }


        /// <summary>
        /// Unique id for the datatype
        /// </summary>
        public override Guid Id { get { return new Guid("562BE1C8-6784-43E2-8524-5B1F7C5C6F52"); } }

        /// <summary>
        /// Name of the datatype
        /// </summary>
        public override string DataTypeName { get { return "Content TreeView"; } }

        #endregion

        #region Constructor

        public ContentTreeDataType()
            : base()
        {
            //set rendercontrol
            base.RenderControl = this._ContentTreeview;

            //init event            
            this._ContentTreeview.Init += new EventHandler(ContentTreeview_Initalised);

            //save event
            base.DataEditorControl.OnSave += new AbstractDataEditorControl.SaveEventHandler(ContentTreeview_Saving);
        }

        #endregion

        #region Control Initalisation

        void ContentTreeview_Initalised(object sender, EventArgs e)
        {
            if (base.Data.Value != null)
                this._ContentTreeview.Selected = base.Data.Value.ToString();
        }

        #endregion

        #region Saving

        void ContentTreeview_Saving(EventArgs e)
        {
            base.Data.Value = this._ContentTreeview.Selected;
        }

        #endregion

    }

}